package com.ruoyi.ahs.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO to describe JSONUtils
 *
 * @author 辛凤文
 * @since 1.0
 */
public class JSONUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @SneakyThrows
    public static String toJsonString(Object o) {
        if (o == null) {
            return null;
        }
        return objectMapper.writeValueAsString(o);
    }

    // String 类型转回 List<Long>
    @SneakyThrows
    public static List<Long> fromJsonToListLong(String jsonString) {
        if (jsonString == null) {
            return null;
        }
        return objectMapper.readValue(jsonString, new TypeReference<List<Long>>() {
        });
    }

    // String 类型转回 List<String>
    @SneakyThrows
    public static List<String> fromJsonToListString(String jsonString) {
        if (jsonString == null) {
            return null;
        }
        return objectMapper.readValue(jsonString, new TypeReference<List<String>>() {
        });
    }

    // String 类型转回 Map<String, List<Long>>
    @SneakyThrows
    public static Map<String, List<Long>> fromJsonToMapStringListLong(String jsonString) {
        if (jsonString == null) {
            return null;
        }
        return objectMapper.readValue(jsonString, new TypeReference<Map<String, List<Long>>>() {
        });
    }

    // String 类型转回 Map<String, List<String>>
    @SneakyThrows
    public static Map<String, List<String>> fromJsonToMapStringListString(String jsonString) {
        if (jsonString == null) {
            return null;
        }
        return objectMapper.readValue(jsonString, new TypeReference<Map<String, List<String>>>() {
        });
    }

    public static void main(String[] args) {
        // 示例：List<Long> 转 JSON
        List<Long> listLong = Arrays.asList(1L, 2L, 3L, 4L);
        String jsonListLong = toJsonString(listLong);
        System.out.println("JSON List<Long>: " + jsonListLong);

        // 从 JSON 转回 List<Long>
        List<Long> parsedListLong = fromJsonToListLong(jsonListLong);
        System.out.println("Parsed List<Long>: " + parsedListLong);

        // 示例：List<String> 转 JSON
        List<String> listString = Arrays.asList("a", "b", "c");
        String jsonListString = toJsonString(listString);
        System.out.println("JSON List<String>: " + jsonListString);

        // 从 JSON 转回 List<String>
        List<String> parsedListString = fromJsonToListString(jsonListString);
        System.out.println("Parsed List<String>: " + parsedListString);

        // 示例：Map<String, List<Long>> 转 JSON
        Map<String, List<Long>> mapStringListLong = new HashMap<>();
        mapStringListLong.put("key1", Arrays.asList(1L, 2L, 3L));
        mapStringListLong.put("key2", Arrays.asList(4L, 5L, 6L));
        String jsonMapStringListLong = toJsonString(mapStringListLong);
        System.out.println("JSON Map<String, List<Long>>: " + jsonMapStringListLong);

        // 从 JSON 转回 Map<String, List<Long>>
        Map<String, List<Long>> parsedMapStringListLong = fromJsonToMapStringListLong(jsonMapStringListLong);
        System.out.println("Parsed Map<String, List<Long>>: " + parsedMapStringListLong);

        // 示例：Map<String, List<String>> 转 JSON
        Map<String, List<String>> mapStringListString = new HashMap<>();

        mapStringListString.put("key1", Arrays.asList("a", "b", "c"));
        mapStringListString.put("key2", Arrays.asList("x", "y", "z"));
        String jsonMapStringListString = toJsonString(mapStringListString);
        System.out.println("JSON Map<String, List<String>>: " + jsonMapStringListString);

        // 从 JSON 转回 Map<String, List<String>>
        Map<String, List<String>> parsedMapStringListString = fromJsonToMapStringListString(jsonMapStringListString);
        System.out.println("Parsed Map<String, List<String>>: " + parsedMapStringListString);
    }
}
